﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;

namespace HotelManagement._03.DAO
{
    class CustomerDAO
    {
        DbConnection m_conn;
        SqlCommand m_command;
        SqlDataAdapter m_adap;

        public CustomerDAO()
        {
            m_conn = new DbConnection();
        }
        public DataTable ViewCustomer(string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
        public DataTable ViewCustomerById(int ip_Id,string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@CustomerID", SqlDbType.BigInt).Value = ip_Id;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
        public bool AddCustomer(Customer ip_Customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                //m_command.Parameters.Add("@CustomerID", SqlDbType.BigInt).Value = ip_Customer.CustomerID;
                m_command.Parameters.Add("@Address", SqlDbType.NVarChar).Value = ip_Customer.Address;
                m_command.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = ip_Customer.Birthday;
                m_command.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = ip_Customer.CustomerName;
                m_command.Parameters.Add("@CustomerTypeID", SqlDbType.BigInt).Value = ip_Customer.CustomerTypeID;
                m_command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ip_Customer.Email;
                m_command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = ip_Customer.Gender;
                m_command.Parameters.Add("@IDCardNo", SqlDbType.VarChar).Value = ip_Customer.IDCardNo;
                m_command.Parameters.Add("@PassportNo", SqlDbType.VarChar).Value = ip_Customer.PassportNo;
                m_command.Parameters.Add("@Note", SqlDbType.NVarChar).Value = ip_Customer.Note;
                m_command.Parameters.Add("@Nationality", SqlDbType.NVarChar).Value = ip_Customer.Nationality;
                m_command.Parameters.Add("@Phone", SqlDbType.VarChar).Value = ip_Customer.Phone;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;

        }
        public bool UpdateCustomer(Customer ip_Customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerID", SqlDbType.BigInt).Value = ip_Customer.CustomerID;
                m_command.Parameters.Add("@Address", SqlDbType.NVarChar).Value = ip_Customer.Address;
                m_command.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = ip_Customer.Birthday;
                m_command.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = ip_Customer.CustomerName;
                m_command.Parameters.Add("@CustomerTypeID", SqlDbType.BigInt).Value = ip_Customer.CustomerTypeID;
                m_command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ip_Customer.Email;
                m_command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = ip_Customer.Gender;
                m_command.Parameters.Add("@IDCardNo", SqlDbType.VarChar).Value = ip_Customer.IDCardNo;
                m_command.Parameters.Add("@PassportNo", SqlDbType.VarChar).Value = ip_Customer.PassportNo;
                m_command.Parameters.Add("@Note", SqlDbType.NVarChar).Value = ip_Customer.Note;
                m_command.Parameters.Add("@Nationality", SqlDbType.NVarChar).Value = ip_Customer.Nationality;
                m_command.Parameters.Add("@Phone", SqlDbType.VarChar).Value = ip_Customer.Phone;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;

        }
        public bool DeleteCustomer(Customer ip_Customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerID", SqlDbType.BigInt).Value = ip_Customer.CustomerID;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }
        public DataTable Search(string ip_KeyWord, string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@KeyWord", SqlDbType.NVarChar).Value = ip_KeyWord;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
        public bool Find(Customer ip_Customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@passport ", SqlDbType.VarChar).Value = ip_Customer.PassportNo;
                m_adap = new SqlDataAdapter(m_command);
                DataTable v_dt = new DataTable();
                m_adap.Fill(v_dt);
                m_conn.Close();
                if (v_dt.Rows.Count > 0)
                    return true;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return false;
        }

        public DataTable SearchCustomer(Customer ip_Customer, String ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerName ", SqlDbType.NVarChar).Value = ip_Customer.CustomerName;
                // m_command.Parameters.Add("@Birthday ", SqlDbType.NVarChar).Value = customer.Birthday.ToString();
                m_command.Parameters.Add("@Gender ", SqlDbType.NVarChar).Value = ip_Customer.Gender;
                m_command.Parameters.Add("@Nationality ", SqlDbType.NVarChar).Value = ip_Customer.Nationality;
                m_command.Parameters.Add("@Address ", SqlDbType.NVarChar).Value = ip_Customer.Address;
                m_command.Parameters.Add("@Phone ", SqlDbType.NVarChar).Value = ip_Customer.Phone;
                m_command.Parameters.Add("@Email ", SqlDbType.NVarChar).Value = ip_Customer.Email;
                m_command.Parameters.Add("@IDCardNo  ", SqlDbType.NVarChar).Value = ip_Customer.IDCardNo;
                m_command.Parameters.Add("@PassportNo ", SqlDbType.NVarChar).Value = ip_Customer.PassportNo;
                m_command.Parameters.Add("@CustomerTypeID ", SqlDbType.NVarChar).Value = ip_Customer.CustomerTypeID;
                m_adap = new SqlDataAdapter(m_command);

                DataTable v_dt = new DataTable();
                m_adap.Fill(v_dt);
                m_conn.Close();
                return v_dt;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return null;
        }
    }
}
